package com.webdemo02.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.webdemo02.actionform.UserForm;
import com.webdemo02.util.DBHandler;

public class QueryUser extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql = "select id,name from xml01_user";
		Connection conn = DBHandler.newInstance().getConnection();
		PreparedStatement pstmt  = conn.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		List<UserForm> users = new ArrayList<UserForm>();
		while(rs.next()){
			int id = rs.getInt("id");
			String name = rs.getString("name");
			UserForm user =new UserForm();
			user.setId(id);
			user.setName(name);
			users.add(user);
		}
		request.setAttribute("users", users);
		return mapping.findForward("show");
	}

}
